#
#  Copyright 2008 Garth Williams
#
#  This file is part of CommandPost.
#
#  CommandPost is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  CommandPost is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with CommandPost.  If not, see <http://www.gnu.org/licenses/>.
#

class ReportTypesController < ApplicationController
  before_filter :setup
  
  def setup
    @section = 'report_types'
  end
  
  # GET /report_types
  # GET /report_types.xml
  def index
    @report_types = ReportType.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @report_types }
    end
  end

  # GET /report_types/1
  # GET /report_types/1.xml
  def show
    @report_type = ReportType.find(params[:id])

    respond_to do |format|
      format.html { render :layout => false } # show.html.erb
      format.xml  { render :xml => @report_type }
    end
  end

  # GET /report_types/new
  # GET /report_types/new.xml
  def new
    @report_type = ReportType.new

    respond_to do |format|
      format.html { render :layout => false }# new.html.erb
      format.xml  { render :xml => @report_type }
    end
  end

  # GET /report_types/1/edit
  def edit
    @report_type = ReportType.find(params[:id])
    @roles = Role.find(:all, :order => 'name')
    
    render :layout => false
  end

  # POST /report_types
  # POST /report_types.xml
  def create
    @report_type = ReportType.new(params[:report_type])
    @report_type.created_by_user_id = session[:user_id]

    respond_to do |format|
      if @report_type.save
        flash[:notice] = 'ReportType was successfully created.'
        format.html { list_types }
        format.xml  { render :xml => @report_type, :status => :created, :location => @report_type }
      else
        format.html { render :action => "new", :layout => false, :status => 500 }
        format.xml  { render :xml => @report_type.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def list_types
    @report_types = ReportType.find(:all)
    render :partial => 'list_types'
  end

  # PUT /report_types/1
  # PUT /report_types/1.xml
  def update
    @report_type = ReportType.find(params[:id])
    @report_type.created_by_user_id = session[:user_id]

    respond_to do |format|
      if @report_type.update_attributes(params[:report_type])
        flash[:notice] = 'ReportType was successfully updated.'
        format.html { list_types }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit", :layout => false }
        format.xml  { render :xml => @report_type.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def set_first
    @report_type = ReportType.find(params[:id])
    @report_type.first_stage_id = params[:stage_id]
    @report_type.created_by_user_id = session[:user_id]
    @report_type.save

    respond_to do |format|
      if @report_type.save
        flash[:notice] = 'First stage was successfully updated.'
        format.html { render :layout => false }
        format.xml  { head :ok }
      else
        format.html { render :layout => false, :status => 500 }
        format.xml  { render :xml => @report_type.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /report_types/1
  # DELETE /report_types/1.xml
  def destroy
    @report_type = ReportType.find(params[:id])
    @report_type.created_by_user_id = session[:user_id]
    @report_type.destroy

    respond_to do |format|
      format.html { list_types }
      format.xml  { head :ok }
    end
  end
end
